package com.mini.task;

import com.mini.constant.RedisKeyConst;
import com.mini.model.BusinessTraditionChance;
import com.mini.repository.BusinessTraditionChanceRepository;
import com.mini.service.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author shixiaopeng  on 2021/2/1
 */
@Component
@EnableScheduling
@Configuration
@Slf4j
public class NextDayTranditionalCallAndWechatNumTask extends BaseService {

    @Autowired
    private BusinessTraditionChanceRepository businessTraditionChanceRepository;

    @Scheduled(cron = "${tradition.updatecallandchatnum.time}")
    public void updateCallAndWechatNum() {
        String opreationLock = RedisKeyConst.getOpreationLock("updateCallAndChatNum");
        if (tryLock(opreationLock)) {
            try {
                log.info("次日变更拨打次数和聊天几次");
                businessTraditionChanceRepository.updateTodayCallNum();
                businessTraditionChanceRepository.updateTodayChatNum();

            } catch (Exception ex) {
                log.error("task-updateCallAndChatNum error ,ex = {} ", ex);
                releaseLockKey(opreationLock);
            } finally {
                releaseLockKey(opreationLock);
            }
        }
    }

}
